package com.kc.sjwh.action;

import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.kc.sjwh.entity.po.Jqbzzd;
import com.kc.sjwh.other.AjaxRes;
import com.kc.sjwh.service.JqbzzdService;

/**
 * 警情标注字典
 * @author zhouy
 *
 */
@Controller
@RequestMapping("jqbzzd")
public class JqbzzdAction extends BaseAction{
	static Logger logger = Logger.getLogger(JqxzAction.class);

	@Autowired
	JqbzzdService jqbzzdService;
	
	/**
	 * 打开页面
	 * @param req
	 * @return
	 */
	@RequestMapping("list")
	public String list(HttpServletRequest req) {
		return "jqbzzd/jqbzzdList";
	}
	
	/**
	 * 获得资源树
	 * @param req
	 * @param yyid
	 * @return
	 */
	@ResponseBody
	@RequestMapping("tree_getTree")
	public String tree_getTree(HttpServletRequest req) {
		return JSON.toJSONString(new AjaxRes(jqbzzdService.tree()));
	}
	
	/**
	 * 获取单个信息
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping("tree_getOne")
	public String tree_getOne(HttpServletRequest req) {
		String bzbh = req.getParameter("bzbh");
		req.getSession().setAttribute("bzbh", bzbh);
		return JSON.toJSONString(new AjaxRes(jqbzzdService.getOne(bzbh)));
	}
	
	/**
	 * 清除session保存的数据
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping("clearSession")
	public String clearSession(HttpServletRequest req){
		req.getSession().removeAttribute("bzbh");
		return JSON.toJSONString(new AjaxRes());
	}
	
	/**
	 * 删除资源
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delete")
	public String delete(HttpServletRequest req) {
		AjaxRes ajaxRes = new AjaxRes();
		jqbzzdService.delete(req.getParameter("bzbh"));
		return JSON.toJSONString(ajaxRes);
	}
	
	/**
	 * 保存编辑
	 * @param req
	 * @param one
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("save")
	public String save(HttpServletRequest req, Jqbzzd one) throws Exception {
		if(StringUtils.isEmpty(one.getBzbh())) {
			String id = UUID.randomUUID().toString().replace("-", "");
			one.setBh(id);
			jqbzzdService.addInfo(one);
		}else{
			one.setGxsj(new Date());
			jqbzzdService.updateJqbzzdInfo(one);
		}
		return JSON.toJSONString(new AjaxRes());
	}
}
